package magicloan.auctionpocket.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import magicloan.auctionpocket.R;

/**
 * Created by Administrator on 2016/7/5.
 */
public class VerticalTextViewHelper {

    private Context mContext ;
    private ArrayList<SpannableString> data;
    private OnSwitchTvClickListener mOnSwitchTvClickListener ;

    public VerticalTextViewHelper(Context context) {
        mContext = context ;
    }

    // 索引
    private int index;
    private boolean isFirst = true;

    // 文本数组
    private TextSwitcher ts ;
    public void initSwitcher(TextSwitcher switcher,OnSwitchTvClickListener onSwitchTvClickListener) {
        if ( ts != null )
            return;
        mOnSwitchTvClickListener = onSwitchTvClickListener ;
        //定义文字切换器
        ts = switcher ;
        //定义视图显示工厂，并设置
        ts.setFactory(new ViewSwitcher.ViewFactory() {

            public View makeView() {
                TextView view = new TextView(mContext);
                view.setTextSize(15);
                view.setEllipsize(TextUtils.TruncateAt.END);
                view.setTextColor(mContext.getResources().getColor(R.color.color_333333));
                FrameLayout.LayoutParams lp = new
                        FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                lp.gravity = Gravity.LEFT;
                view.setLayoutParams(lp);
                return view;
            }
        });

        ts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnSwitchTvClickListener!=null)
                    mOnSwitchTvClickListener.onClick(index);
            }
        });

//        if ( isFirst ){
//            String temp = "成都" + "的" + "赵" + "先生" + "刚申请的" + bean.getProductName() +  bean.getPersonLoan() + "元已经放款成功";
//            SpannableString spanableInfo = new SpannableString(temp);
//            int start = temp.indexOf("请的") + 2 ;  //超链接起始位置
//            int end = start + bean.getProductName().length() ;   //超链接结束位置
//            spanableInfo.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //tv_black
//            ForegroundColorSpan greenSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_orange_color));
//            spanableInfo.setSpan(greenSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            mDataList.add(spanableInfo);
//        }

        isFirst = false ;

        // 设置切入动画
        ts.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_bottom));
        // 设置切出动画
        ts.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_out_top));

        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                tvHandler.sendEmptyMessage(0);
            }
        };
        timer.schedule(task,0,3000);

    }

    /**
     * "id": 0,
     "loanUserName": "t先生",
     "age": 30,
     "amount": 30000,
     "managerUserName": null,
     "invest": false
     */
    Handler tvHandler = new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            if ( data == null ||  data.size() <= 0 )
                return;
            index++;
            if (index >= data.size()) {
                index = 0;
            }
//            JSONObject currentObj = data.optJSONObject(index);
//            String text = currentObj.optString("loanUserName")+","+currentObj.optString("age")+"岁,贷款金额 "+currentObj.optString("amount")+"元";
//            if ( !"null".equals(currentObj.optString("managerUserName")) )
//                text = text +"\n--"    + currentObj.optString("managerUserName") +"已接单" ;
//            ts.setText(text);
            ts.setText(data.get(index));
        }
    };

    public void setData(ArrayList<SpannableString> data) {
        this.data = data;
    }


    private class MyTextSwitcher extends TextSwitcher{

        public MyTextSwitcher(Context context) {
            super(context);
        }

        public MyTextSwitcher(Context context, AttributeSet attrs) {
            super(context, attrs);
        }


    }

    //----------设置点击时候接口
    public interface OnSwitchTvClickListener{
        public void onClick(int position);
    }

}
